<?
/*----------------------------------------------------------------------
          (C) Bobtail-Bear 2006 	小飞熊新一代下载系统
          	代码编写：小飞熊	版权所有：小飞熊[L.F.Bear]
				官方网站/技术支持：http://www.lfbear.cn
------------------------------------------------------------------------
          (C) Bobtail-Bear 2006 	L.F.Bear's New Download Sysytem
          	Powered by L.F.Bear  Copyright L.F.Bear's Home(R)
               Official Web/Support：http://www.lfbear.cn
-----------------------------------------------------------------------*/

//本页脚本功能：分类管理
$thispage="class.php";
echo ("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
require("../function.php");
require("check.php");
$classfile="../include/class.php";
$subclassfile="../include/subclass.php";

if ($_COOKIE[status]=="login" && $_SESSION[login]=="yes")
{
$yourname=$_COOKIE[yourname];
$level=$_SESSION[level];
if(!checklevel($level,"soft"))
	{$msg_title="系统信息：权限不足";
	$msg_link="login.php";
	$msg="您无法进行此项管理操作！<br><br>原因：您所在的用户组没有该权限。<br><br>";
	require("error.php");
	weblog("分类管理","无权限操作，被系统拦截！","red",$yourname,"触发页面：class.php");
	die;}
	

	
//////预备
if (file_exists($classfile)) {
	$classdata=file($classfile);
	$count=count($classdata);
}
if (file_exists($subclassfile)) {
	$subclassdata=file($subclassfile);
	$subcount=count($subclassdata);
}


	$classselect="";
	$list=@file($classfile);
$count1=count($list)-1;
for ($i=0; $i<=$count1; $i++) {
	$list_info=explode("|",$list[$i]);
	$classselect.=" <option value=\"$list_info[0]\">$list_info[1]</option>";
}


	unset($list_info);
	$subclassselect="";
	$list=file($subclassfile);
$count2=count($list)-1;
for ($i=0; $i<=$count2; $i++) {
	$list_info=explode("|",$list[$i]);
	$subclassselect.=" <option value=\"$list_info[0]\">$list_info[2]</option>";
}


///Ready is OK
$action=$_GET[action];
switch ($action){

case plus:

if (file_exists($classfile)==0) {
$fp=fopen($classfile, "w");
	if ($fp==0) 
	{$msg_title="系统信息：操作中止";
	$msg_link="class.php?action=plus";
	$msg="您无法进行此项管理操作！<br><br>无数据库文件，系统自动建立数据库时失败，请确认include目录的可写性！<br><br>";
	require("error.php");
	weblog("分类管理","无数据库文件(无法建立include/class.php)","red",$yourname,"触发页面：class.php?action=plus");
	die;}
	else {$msg_title="系统信息：系统提示";
	$msg_link="class.php?action=plus";
	$msg="无数据库文件！<br><br>系统自动给予建立…数据库文件建立成功！<br>系统没有主分类，请您先建立一个主分类。<br><br>";
	require("ok.php");
	weblog("分类管理","无数据库文件(自动建立include/class.php)","green",$yourname,"触发页面：class.php?action=plus");
	die;}
}

$act="?".$action;
$msg_="重要信息";
$msg_color="#FF8C1A";
require("msg.php");
print <<<EOT


<link href="images/css.css" rel="stylesheet" type="text/css">
<style type="text/css">
<!--
.massage{color:#993399;
cursor:help;
text-decoration:none;}
.subtitle{
color:#3300FF;
font-family:"宋体";
font-size:12px;
font-weight:bold;}
-->
</style>
<body bgcolor="#DDF1FF">
<table width="750" border="1" align="center" cellpadding=4 cellspacing=0 bordercolor="#ACC1FC" heoght=400>
  <tr bgcolor='#ACC1FC'>
    <td colspan="3"><a class="title">欢迎您：$yourname 登陆管理程序</a></td>
  </tr>
  <tr bgcolor='#FFFFFF'>
    <td colspan="3" bgcolor="#EBE9ED" class="comments-title">分类管理--&gt;添加分类</td>
  </tr>
    <tr bgcolor='#FFFFFF'>
    <td width="100" bgcolor="#FFFFFF" class="subtitle">主分类操作</td>
    <td colspan="2" bgcolor="#FFFFFF">&nbsp;</td>
  </tr>
    <form action="class.php?action=newclass" method=post>
	<tr bgcolor='#FFFFFF'>
      <td bgcolor="#FFFFFF">新的主分类名称：</td>
      <td width="538" bgcolor="#FFFFFF"><input name="mainclass" type="text" id="mainclass" size="30" />
      &nbsp;&nbsp;文字说明：      
      <input name="class_intro" type="text" id="class_intro" size="30" /></td>
      <td width="80" bgcolor="#FFFFFF"><div align="center">
        <input name="Submit" type="submit" id="Submit" value="提交">
      </div></td>
    </tr>
    <tr bgcolor='#FFFFFF'>
      <td bgcolor="#FFFFFF" class="subtitle">子分类操作</td>
      <td colspan="2" bgcolor="#FFFFFF">&nbsp;</td>
    </tr></form>
	<form action="class.php?action=newsubclass" method=post>
    <tr bgcolor='#FFFFFF'>
      <td bgcolor="#FFFFFF">新的子分类名称：</td>
      <td bgcolor="#FFFFFF"><input name="subclass" type="text" id="path_user" size="20">
        添加到
          <select name="classin" id="classin">$classselect
        </select>
       &nbsp;&nbsp;文字说明：
        <input name="subclass_intro" type="text" id="subclass_intro" size="20"></td>
      <td bgcolor="#FFFFFF"><div align="center">
        <input name="Submit" type="submit" id="Submit" value="提交">
      </div></td>
    </tr></form>
  <tr bgcolor='#FFFFFF'>
    <td colspan="3">&nbsp;</td>
  </tr>
</table>

EOT;
require("footer.php");
exit;
die;
break;

case newclass:
$class=$_POST[mainclass];
$class_intro=$_POST[class_intro];
$class=ckchar($class);
$class_intro=ckchar($class_intro);
if (!file_exists($classfile)){
	$msg_title="系统信息：严重错误";
	$msg_link="class.php?action=plus";
	$msg="您无法进行此项管理操作！<br><br>数据文件丢失，点击“确定”重新建立分类数据库文件！<br><br>";
	weblog("分类管理","无数据库文件（需重新恢复include/class.php）","red",$yourname,"触发页面：class.php?action=plus");
	require("error.php");
	die;}
$mcount=$count+1;
$date=date(Ymd);
if($class=="") $class="未命名主分类";
if($class_intro=="") $class_intro="没有文字说明";
$info="$mcount|$class|$class_intro|$date\n";
writef($classfile,$info,"a");
	{$msg_title="系统信息：操作成功";
	$msg_link="class.php?action=plus";
	$msg="主分类: $class 已经成功添加。<br><br>点击“确定”继续添加。<br><br>";
	weblog("分类管理","成功添加主分类 $class","green",$yourname,"触发页面：class.php?action=plus");
	require("ok.php");
	die;}
break;

case newsubclass:
$scount=$subcount+1;
$date=date(Ymd);
$sub=$_POST[classin];
$subclass=$_POST[subclass];
$subclass=ckchar($subclass);
$subclass_intro=$_POST[subclass_intro];
$subclass_intro=ckchar($subclass_intro);
if($subclass=="") $subclass="未命名子分类";
if($subclass_intro=="") $subclass_intro="没有文字说明";
$info="$scount|$sub|$subclass|$subclass_intro|$date\n";
writef("../include/subclass.php",$info,"a");
	{$msg_title="系统信息：操作成功";
	$msg_link="class.php?action=plus";
	$msg="子分类: $subclass 已经成功添加。<br><br>点击“确定”继续添加。<br><br>";
	weblog("分类管理","成功添加子分类 $class","green",$yourname,"触发页面：class.php?action=plus");
	require("ok.php");
	die;}
break;

case edit:


$act="?".$action;
$msg_="重要信息";
$msg_color="#FF8C1A";
require("msg.php");
print <<<EOT



<link href="images/css.css" rel="stylesheet" type="text/css">
<style type="text/css">
<!--
.massage{color:#993399;
cursor:help;
text-decoration:none;}
.subtitle{
color:#3300FF;
font-family:"宋体";
font-size:12px;
font-weight:bold;}
-->
</style>
<SCRIPT language=javascript>
<!--
var t=0;
-->
</SCRIPT>
<body bgcolor="#DDF1FF">
<table width="750" border="1" align="center" cellpadding=4 cellspacing=0 bordercolor="#ACC1FC" heoght=400>
  <tr bgcolor='#ACC1FC'>
    <td colspan="3"><a class="title">欢迎您：$yourname 登陆管理程序</a></td>
  </tr>
  <tr bgcolor='#FFFFFF'>
    <td colspan="3" bgcolor="#EBE9ED" class="comments-title">分类管理--&gt;管理分类</td>
  </tr>
    <tr bgcolor='#FFFFFF'>
    <td width="100" bgcolor="#FFFFFF" class="subtitle">主分类操作</td>
    <td colspan="2" bgcolor="#FFFFFF">&nbsp;</td>
  </tr>
    <form action="class.php?action=edititem" method=post>
	<tr bgcolor='#FFFFFF'>
      <td bgcolor="#FFFFFF">1、分类更名：</td>
      <td width="538" bgcolor="#FFFFFF">将分类
        <select name="item" id="item">
        $classselect</select>
        更名为
        <input name="name" type="text" id="name" size="20">
      文字说明：      
      <input name="intro" type="text" id="intro" size="20" /></td>
      <td width="80" bgcolor="#FFFFFF"><div align="center">
        <input name="Submit" type="submit" id="Submit" value="提交">
      </div></td>
    </tr></form>
	<form action="class.php?action=modifyorder" method=post>
	<tr bgcolor='#FFFFFF'>
	  <td bgcolor="#FFFFFF">2、分类排序：</td>
	  <td width="538" bgcolor="#FFFFFF">将分类
	    <select name="item" id="item">
        $classselect</select>
	    放到
	    <select name="item2" id="item2">
        $classselect</select>
	    的后面</td>
	  <td width="80" bgcolor="#FFFFFF"><div align="center">
        <input name="Submit" type="submit" id="Submit" value="提交">
      </div></td>
	</tr></form>
	<form action="class.php?action=delitem" method=post>
	<tr bgcolor='#FFFFFF'>
	  <td bgcolor="#FFFFFF">3、分类删除：</td>
	  <td width="538" bgcolor="#FFFFFF">删除
	    <select name="item" id="item">
         $classselect</select>
	    分类&nbsp;&nbsp;（仅删除分类，不会删除其下属软件）</td>
	  <td width="80" bgcolor="#FFFFFF"><div align="center">
        <input name="Submit" type="submit" id="Submit" value="提交">
      </div></td>
	</tr></form>
    <tr bgcolor='#FFFFFF'>
      <td bgcolor="#FFFFFF" class="subtitle">子分类操作</td>
      <td colspan="2" bgcolor="#FFFFFF">&nbsp;</td>
    </tr>
	<form action="class.php?action=editsubitem" method=post>
    <tr bgcolor='#FFFFFF'>
      <td bgcolor="#FFFFFF">1、分类更名：</td>
      <td bgcolor="#FFFFFF">将分类
        <select name="subitem" id="subitem">$subclassselect
                        </select>
更名为
<input name="name" type="text" id="name" size="15">
文字说明：
<input name="intro" type="text" id="intro" size="15" /></td>
      <td bgcolor="#FFFFFF"><div align="center">
        <input name="Submit" type="submit" id="Submit" value="提交">
      </div></td>
    </tr></form>
	<form action="class.php?action=modifysuborder" method=post>
    <tr bgcolor='#FFFFFF'>
      <td bgcolor="#FFFFFF">2、分类排序：</td>
      <td bgcolor="#FFFFFF">将分类
        <select name="item1" id="item1">
                        $subclassselect</select>
放到
<select name="item2" id="item2">
$subclassselect</select>
的后面</td>
      <td bgcolor="#FFFFFF"><div align="center">
        <input name="Submit" type="submit" id="Submit" value="提交">
      </div></td>
    </tr></form>
	<form action="class.php?action=delsubitem" method=post>
    <tr bgcolor='#FFFFFF'>
      <td bgcolor="#FFFFFF">3、删除分类：</td>
      <td bgcolor="#FFFFFF">删除
        <select name="subitem" id="subitem">
               $subclassselect </select>
分类 （删除前请将该分类下的软件转移到其他分类下）</td>
      <td bgcolor="#FFFFFF"><div align="center">
        <input name="Submit" type="submit" id="Submit" value="提交">
      </div></td>
    </tr></form><form action="class.php?action=moveitem" method=post>
    <tr bgcolor='#FFFFFF'>
      <td bgcolor="#FFFFFF">4、软件转移：</td>
      <td bgcolor="#FFFFFF">将
        <select name="subitem1" id="subitem1">
                $subclassselect</select>
        分类下的软件转移到
        <select name="subitem2" id="subitem2">
               $subclassselect </select>
        分类下 （所需操作时间视软件多少而定）</td>
      <td bgcolor="#FFFFFF"><div align="center">
        <input name="Submit" type="submit" id="Submit" value="提交" onclick="t=t+1;if (t<=1){alert('笨笨熊提醒您：转移软件通常需要很长时间，并且转移完成后需要全部重建软件列表，如非必要请不要做此操作！我只提醒你一次哦～'); return false;}">
      </div></td>
    </tr>
	</form>
  <tr bgcolor='#FFFFFF'>
    <td colspan="3">&nbsp;</td>
  </tr>
</table>


EOT;
require("footer.php");
exit;
die;
break;




//////////////////主分类编辑部分函数///////////////////

case edititem:
{
$time=date(Ymd);
$item=$_POST[item];
$name=$_POST[name];
$name=ckchar($name);
$intro=$_POST[intro];
$intro=ckchar($intro);
unset($temp);
unset($newstring);
	for ($i=0; $i<$count; $i++) {
			$temp=explode("|",$classdata[$i]);
			if($temp[0]==$item){ 
		 if($intro=="") $intro=$temp[2];
          $classdata[$i]="$item|$name|$intro|$time\n";
		  }
			$newstring.="$classdata[$i]";
		}
	writef($classfile,$newstring,"w");				        
	
	{$msg_title="系统信息：操作成功";
	$msg_link="class.php?action=edit";
	$msg=$name."(主分类)已经成功更名。<br><br>点击“确定”继续操作。<br><br>";
	weblog("分类管理","主分类更名成功","green",$yourname,"触发页面：class.php?action=edit");
	require("ok.php");
	die;}
}
break;

case modifyorder: 
{
$item=$_POST[item];
$item2=$_POST[item2];
unset($temp);
unset($temp2);
unset($newstring);
if($item==$item2)
	{$msg_title="系统信息：无效操作";
	$msg_link="class.php?action=edit";
	$msg="您没有变更任何顺序。<br><br>点击“确定”重新操作。<br><br>";
	require("ok.php");
	die;}
else{
		for ($i=0; $i<$count+1; $i++) {
			$temp=explode("|",$classdata[$i]);
			if($temp[0]!= $item) $newstring.=$classdata[$i];
		  if($temp[0] == $item2){
			  for ($j=0; $j<$count+1; $j++) {
	       $temp2=explode("|",$classdata[$j]);
	       if ($temp2[0]==$item) $newstring.=$classdata[$j];
	    }}}
	writef($classfile,$newstring,"w");			
	{$msg_title="系统信息：操作成功";
	$msg_link="class.php?action=edit";
	$msg="主分类次序变更成功。<br><br>点击“确定”继续操作。<br><br>";
	weblog("分类管理","主分类更序成功","green",$yourname,"触发页面：class.php?action=edit");
	require("ok.php");
	die;}
}}
break;


case delitem: 
{
$item=$_POST[item];
unset($temp);
	for ($i=0; $i<$count; $i++) {
			$temp=explode("|",$classdata[$i]);
			if($temp[0]==$item){ 
          $classdata[$i]="";
		  }
			$newstring.="$classdata[$i]";
		}
	writef($classfile,$newstring,"w");				        
	
	{$msg_title="系统信息：操作成功";
	$msg_link="class.php?action=edit";
	$msg=$temp[1]."(主分类)已经成功删除。<br><br>点击“确定”继续操作。<br><br>";
	weblog("分类管理","主分类删除成功","green",$yourname,"触发页面：class.php?action=edit");
	require("ok.php");
	die;}
}
break;




//////////////////子分类编辑部分函数///////////////

case editsubitem:
{
$time=date(Ymd);
$subitem=$_POST[subitem];
$name=$_POST[name];
$name=ckchar($name);
$intro=$_POST[intro];
$intro=ckchar($intro);
unset($temp);
unset($newstring);

	for ($i=0; $i<$subcount; $i++) {
			$temp=explode("|",$subclassdata[$i]);
			if($temp[0]==$subitem){ 
		 if($intro=="") $intro=$temp[3];
          $subclassdata[$i]="$subitem|$temp[1]|$name|$intro|$time\n";
		  }
			$newstring.="$subclassdata[$i]";
		}
	writef($subclassfile,$newstring,"w");				        
	
	{$msg_title="系统信息：操作成功";
	$msg_link="class.php?action=edit";
	$msg=$name."(子分类)已经成功更名。<br><br>点击“确定”继续操作。<br><br>";
	weblog("分类管理","子分类更名成功","green",$yourname,"触发页面：class.php?action=edit");
	require("ok.php");
	die;}
}
break;


case modifysuborder: 
{
$item1=$_POST[item1];
$item2=$_POST[item2];
unset($temp);
unset($temp2);
unset($newstring);
if($item1==$item2)
	{$msg_title="系统信息：无效操作";
	$msg_link="class.php?action=edit";
	$msg="您没有变更任何顺序。<br><br>点击“确定”重新操作。<br><br>";
	require("ok.php");
	die;}
else{
		for ($i=0; $i<$subcount+1; $i++) {
			$temp=explode("|",$subclassdata[$i]);
			if($temp[0]!= $item1) $newstring.=$subclassdata[$i];
		  if($temp[0] == $item2){
			  for ($j=0; $j<$subcount+1; $j++) {
	       $temp2=explode("|",$subclassdata[$j]);
	       if ($temp2[0]==$item1) $newstring.=$subclassdata[$j];
	    }}}
	writef($subclassfile,$newstring,"w");			
	{$msg_title="系统信息：操作成功";
	$msg_link="class.php?action=edit";
	$msg="子分类次序变更成功。<br><br>点击“确定”继续操作。<br><br>";
	weblog("分类管理","子分类更序成功","green",$yourname,"触发页面：class.php?action=edit");
	require("ok.php");
	die;}
}}
break;



case delsubitem: 
{
$subitem=$_POST[subitem];

unset($temp);
	for ($i=0; $i<$subcount; $i++) {
			$temp=explode("|",$subclassdata[$i]);
			if($temp[0]==$subitem){ 
				$del_item=$temp[2];
          $subclassdata[$i]="";
		  }
			$newstring.="$subclassdata[$i]";
		}
	writef($subclassfile,$newstring,"w");				        
	
	{$msg_title="系统信息：操作成功";
	$msg_link="class.php?action=edit";
	$msg=$del_item."(子分类)已经成功删除。<br><br>点击“确定”继续操作。<br><br>";
	weblog("分类管理","子分类删除成功","green",$yourname,"触发页面：class.php?action=edit");
	require("ok.php");
	die;}
}

break;

//软件转移部分（未完成）
case moveitem:
$subitem1=$_POST[subitem1];
$subitem2=$_POST[subitem2];
$thistime=$_GET[this];
require("../include/config.inc.php");
$soft_num=readf("../".$path_soft."/number");
$move_time=ceil($soft_num/100);

switch($thistime)
{

case "":
case 0:



echo "<br>分类转移情况反馈：<br><br>转移将分批进行，每100个软件为一批，需要转换 $move_time 次，转移完成后<br>将需要全部重新建立一次软件列表。<br>";

echo("<Meta http-equiv='Refresh' Content='3; Url=class.php?action=moveitem&this=1&si1=$subitem1&si2=$subitem2'>");

echo ("<br><br> <a href=class.php?action=moveitem&this=1&si1=$subitem1&si2=$subitem2 title=\"如果不能自动跳转请点击这里\">准备开始转移分类…</a></font>");

break;
default:

$subitem1=$_GET[si1];
$subitem2=$_GET[si2];

echo "<br>分类转移情况反馈：<br><br>";

for($i=($thistime-1)*100+1;$i<=$thistime*100;$i++)
{
	if(file_exists("../".$path_soft."/".$i.".dat"))
	{
	unset($temp);
	$temp=readf("../".$path_soft."/".$i.".dat");
	$temp=explode("|",$temp);
	if($temp[7]==$subitem1) 
	{
	$temp[7]=$subitem2;
	$temp=implode("|",$temp);
	writef("../".$path_soft."/".$i.".dat",$temp,"w");
	unset($tempx);
	$tempx=readf("../".$path_soft."/".$i.".php");
	$tempx=explode("|",$tempx);
	$tempx[7]=$subitem2;
	$tempx=implode("|",$tempx);
	writef("../".$path_soft."/".$i.".php",$tempx,"w");
	}
	}
}
echo ">>> 第 $thistime 次转移完成，正在计算是否需要下一次转换… <br>";
$thistime++;

if ($thistime<=$move_time)
{
echo("<Meta http-equiv='Refresh' Content='1; Url=class.php?action=moveitem&this=$thistime&si1=$subitem1&si2=$subitem2'>");

echo ("<br><br> <a href=class.php?action=moveitem&this=$thistime&si1=$subitem1&si2=$subitem2 title=\"如果不能自动跳转请点击这里\">准备继续进行下一次转移…</a></font>");
}
else
{
echo("<Meta http-equiv='Refresh' Content='3; Url=rebuild.php?action=soft_list'>");

echo ("<br><br> <a href=rebuild.php?action=soft_list title=\"如果不能自动跳转请点击这里\">全部转移完成，正在准备重建所有软件列表…</a></font>");
}
break;


}

break;


default:
die("Error Parameter,Forbidden Access!");
break;
}
}
?>